*' --------------
* sameness effects: main ----
* Appendix Table A3. robustness analysis using longitudinal sample
*' ==============

clear all
* set up your working directory here
global home_dir ""

cd ${home_dir}

* predicted margins for interaction
cap: program drop margin_est
program margin_est
	args X k mname year vv geo_type
	sum `X', d 
	local gap = (`r(max)' - `r(min)') / `k' 

	margin if e(sample), at(`X' = (`r(min)' (`gap') `r(max)')) predict(pr) post

	matrix A = r(table)
	matrix B = r(at)

	mat2txt, matrix(A) saving("./output/margins/`vv'_margins_est_`mname'_`X'_`year'_`geo_type'.txt") replace
	mat2txt, matrix(B) saving("./output/margins/`vv'_at_est_`mname'_`X'_`year'_`geo_type'.txt") replace

end 

cap: program drop margin_interact
program margin_interact 
	args X Y k mname year vv geo_type
	sum `X', d 
	local gap = (`r(max)' - `r(min)') / `k' 

	margin `Y' if e(sample), at(`X' = (`r(min)' (`gap') `r(max)')) predict(pr) post
	
	matrix A = r(table)
	matrix B = r(at)

	mat2txt, matrix(A) saving("./output/margins/`vv'_margins_interact_`mname'_`X'_`Y'_`year'_`geo_type'.txt") replace
	mat2txt, matrix(B) saving("./output/margins/`vv'_at_interact_`mname'_`X'_`Y'_`year'_`geo_type'.txt") replace

end 

local vv "v1"
local geo_type cz

cap: program drop run_robustness_longitudinal
program run_robustness_longitudinal

	args geo_type vv 
	
	use "./data/processed_notshared/cleaned_regtable_unemployment_`geo_type'_v2.dta", clear 
	
	local year "all"
	
	svyset [pw=weight] 	
	
	if ("`vv'" == "v1"){
		keep if sample_v1 == 1	
	}
	if ("`vv'" == "v2") {
		keep if sample_v2 == 1	
	}
	
	
	local demographics_individual Female agegroup Race6 BornUSA 
	local demographics_county RAT_Female RAT_AgeGrp4_2 RAT_AgeGrp4_3 RAT_AgeGrp4_4 RAT_Race6_2 RAT_Race6_3 RAT_Race6_4 RAT_Race6_5 RAT_Race6_6 RAT_BornUSA 
	local contextual_control pop_density
	
	egen n_miss = rowmiss(Suic St `geo_type' Year `demographics_individual' `demographics_county' `contextual_control')
	recode n_miss (0=0) (1/max=1)
	
	drop if n_miss > 0
	
	gen b_same_prop_empstat = std_same_prop_empstat > 0 if ~missing(std_same_prop_empstat)
	
	*--------------
	* Figure 3. interaction effects : Table S6, Model 1 to 4
	*==============
	local eq_demographics_individual Female i.agegroup i.Race6 BornUSA 
	local eq_demographics_county RAT_Female RAT_AgeGrp4_2 RAT_AgeGrp4_3 RAT_AgeGrp4_4 RAT_Race6_2 RAT_Race6_3 RAT_Race6_4 RAT_Race6_5 RAT_Race6_6 RAT_BornUSA 
	local eq_contextual_control pop_density
	local eq_post_control_context RAT_EducGroup_1 RAT_EducGroup_2 RAT_EducGroup_3 RAT_EducGroup_4 RAT_EducGroup_5 RAT_PhysProb RAT_veteran
	
	local model_eq `eq_demographics_individual' `eq_demographics_county' `eq_contextual_control' 
		
	estimates clear 

	logit Suic i.St i.Year `model_eq' c.std_same_prop_empstat [pw=weight], cluster(`geo_type'), if empstat == 2
	estimates store est1
	
	logit Suic i.St i.Year `model_eq' i.EducGroup i.PhysProb i.veteran  c.std_same_prop_empstat [pw=weight], cluster(`geo_type'), if empstat == 2
	estimates store est2
	
	logit Suic i.St i.Year `model_eq' i.EducGroup i.PhysProb i.veteran  `eq_post_control' c.std_same_prop_empstat [pw=weight], cluster(`geo_type'), if empstat == 2
	estimates store est3
	
	logit Suic i.`geo_type' i.Year `model_eq' i.EducGroup i.PhysProb i.veteran  `eq_post_control' c.std_same_prop_empstat [pw=weight], cluster(`geo_type'), if empstat == 2
	estimates store est4
					
	logit Suic i.`geo_type' `model_eq' i.EducGroup i.PhysProb i.veteran  `eq_post_control' c.std_same_prop_empstat [pw=weight], cluster(`geo_type'), if empstat == 2
	estimates store est5

	reghdfe Suic i.Year `model_eq' i.EducGroup i.PhysProb i.veteran  `eq_post_control' c.std_same_prop_empstat [pw=weight], absorb(`geo_type') cluster(`geo_type'), if empstat == 2
	estimates store est6

	jacknife, dots cluster(`geo_type'): logit Suic i.St i.Year `model_eq' i.EducGroup i.PhysProb i.veteran  `eq_post_control' `eq_interact_context' c.std_same_prop_empstat [pw=weight], cluster(`geo_type'), if empstat == 2
	estimates store est7

	* state extended UI benefits 
	logit Suic i.St i.Year `model_eq' c.std_same_prop_empstat st_ui_benefits st_ui_recipient [pw=weight], cluster(`geo_type'), if empstat == 2
	estimates store est8

	* state-year interaction FE
	reghdfe Suic `model_eq' c.std_same_prop_empstat [pw=weight], absorb(Year St i.Year#i.St) cluster(`geo_type'), if empstat == 2
	estimates store est9

	* binary treatment
	logit Suic i.Year `model_eq' c.b_same_prop_empstat [pw=weight], cluster(`geo_type'), if empstat == 2
	estimates store est10

	esttab est* using "./output/logit_model_v1_empstat_robustness_`geo_type'.csv", csv replace label nogap se star (+ 0.1 * 0.05 ** 0.01) mtitle

	* marginal effects 
	forvalues i = 1 / 7 {
		estimates restore est`i'
		margins, dydx(std_same_prop_empstat) post
		estimates store m`i' 
	}

	forvalues i = 8 / 9 {
		estimates restore est`i'
		margins, dydx(std_same_prop_empstat) post
		estimates store m`i' 
	}
	
	local i = 10
	estimates restore est`i'
	margins, dydx(b_same_prop_empstat) post
	estimates store m`i' 

	esttab m* using "./output/AME_model_v1_empstat_robustness_`geo_type'.csv", csv replace label nogap se star (+ 0.1 * 0.05 ** 0.01) mtitle

end

run_robustness_longitudinal cz "v1" 
run_robustness_longitudinal county "v1"


